package com.mini.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import com.google.common.base.Joiner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;

/**
 * DES :
 * 上传文件的工具类
 */
@Component
@Slf4j
public class UploadAliyunUtil {

    @Value("${aliyun.oss.endpoint}")
    private  String ossEndpoint;

    @Value("${aliyun.oss.accessKeyId}")
    private String ossAccessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private String ossAccessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String ossBucketName;

    @Value("${aliyun.oss.imgdomain}")
    private String imgDomain;


    protected OSSClient getOssClient(){
        OSSClient client = new OSSClient(ossEndpoint,ossAccessKeyId, ossAccessKeySecret);
        return client;
    }

    /**
     * 公共上传文件
     * 举例：
     * modelPath：主目录
     * subModelPath：子目录
     * modelPath：主目录
     * needResize：img
     */
    public String uploadOssFile(File file, String modelPath, String subModelPath) {

        if (file != null) {
            try {
                // 获取文件后缀名
                String resultSuffix = "";
                String originalFileName = file.getName();
                if (StringUtils.isNotBlank(originalFileName)) {
                    int start = originalFileName.lastIndexOf(".");
                    start = start == -1 ? 0 : start;
                    String suffix = originalFileName.substring(start, originalFileName.length());
                    resultSuffix = suffix.startsWith(".") ? suffix : "." + suffix;
                }
                resultSuffix = StringUtils.isBlank(resultSuffix) ? ".jpg" : resultSuffix;

                // 存储路径 格式 /img/yyyy/mmdd/
                String uploadRealPathDir = Joiner.on("/").join(modelPath, subModelPath, CalendarUtil.getDateString(new Date(),CalendarUtil.SHORT_DATA_FORMAT_YYYY_MMDD).concat("/"));
                String localUploadFilePath = uploadRealPathDir + CalendarUtil.getDateString(new Date(),CalendarUtil.SIMPLE_DATE_FORMAT_NO_DASH) + resultSuffix;
                //上传到ali oss
                OSSClient ossClient = this.getOssClient();
                log.info("upload file={}", file);
//                InputStream inputStream = new FileInputStream(file);
                PutObjectResult putObjectResult = ossClient.putObject(ossBucketName, localUploadFilePath, file);
                log.info("putObjectResult={} ={}",putObjectResult.getETag(),putObjectResult.getResponse());

                String imgPath = imgDomain + "/" + localUploadFilePath;

                return imgPath;

            } catch (Exception e) {
                log.error("上传文件报错，{}", e);
                return null;
            }
        } else {
            return null;
        }
    }
    public String uploadOssFile1(File file, String modelPath, String subModelPath) {
        // 存储路径 格式 /img/yyyy/mmdd/
        String uploadRealPathDir = Joiner.on("/").join(modelPath, subModelPath, CalendarUtil.getDateString(new Date(),CalendarUtil.SHORT_DATA_FORMAT_YYYY_MMDD).concat("/"));
        String localUploadFilePath = uploadRealPathDir + file.getName();
        OSSClient ossClient = this.getOssClient();
        log.info("upload file={}", file);
        PutObjectResult putObjectResult = ossClient.putObject(ossBucketName, localUploadFilePath, file);
        log.info("putObjectResult={} ={}",putObjectResult.getETag(),putObjectResult.getResponse());

        String imgPath = imgDomain + "/" + localUploadFilePath;
        ossClient.shutdown();
        return imgPath;
    }


    }
